home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / FZNUM.ZIP / fuzzy / fznum / fznum.cc next >
Encoding:
C/C++ Source or Header  |  1994-10-03  |  22.2 KB  |  1,141 lines

  1. //---------------------------------------------------------------------------
  2. //
  3. // FZNUM.CC
  4. //
  5. //    Fuzzy number type implementation.
  6. //
  7. // Contents:
  8. //
  9. //    FzNum::FzNum( double value)
  10. //    FzNum::FzNum( double left, double middle, double right)
  11. //    FzNUm::FzNum( double lBase, double lTop, double rTop, double rBase)
  12. //
  13. //    double FzNum::operator[]( double value)
  14. //
  15. //    FzNum FzNum::operator-()
  16. //
  17. //    FzNum FzNum::operator+( double opnd2)
  18. //    FzNum FzNum::operator+( FzNum opnd2)
  19. //    FzNum FzNum::operator-( double opnd2)
  20. //    FzNum FzNum::operator-( FzNum opnd2)
  21. //    FzNum FzNum::operator*( double opnd2)
  22. //    FzNum FzNum::operator*( FzNum opnd2)
  23. //    FzNum FzNum::operator/( double opnd2)
  24. //    FzNum FzNum::operator/( FzNum opnd2)
  25. //
  26. //    FzNum FzNum::operator+=( double opnd2)
  27. //    FzNum FzNum::operator-=( double opnd2)
  28. //    FzNum FzNum::operator*=( double opnd2)
  29. //    FzNum FzNum::operator/=( double opnd2)
  30. //
  31. //    FzNum operator/( double opnd1, FzNum opnd2)
  32. //
  33. //    ostream& operator<<( ostream& smStream, FzNum item)
  34. //
  35. //    FzNum min( FzNum opnd1, FzNum opnd2)
  36. //    FzNum max( FzNum opnd1, FzNum opnd2)
  37. //
  38. //    FzNum sin( FzNum opnd)
  39. //    FzNum cos( FzNum opnd)
  40. //
  41. //    double fzSM( FzNum opnd1, FzNum opnd2, double lambda)
  42. //
  43. // Author: S. Deodato ( 03.03.94)
  44. //
  45. //---------------------------------------------------------------------------
  46.  
  47.  
  48. #include "fznum.h"
  49.  
  50. #include <stdlib.h>
  51.  
  52.  
  53. //---------------------------------------------------------------------------
  54. //
  55. // FzNum::FzNum( double value)
  56. //
  57. //    Constructor: convert a singleton into a fuzzy number.
  58. //
  59. // Arguments:
  60. //
  61. //    double value
  62. //       singleton to convert
  63. //
  64. // Return value:
  65. //
  66. //    a new fuzzy number
  67. //
  68. // Side effects:
  69. //
  70. //    none
  71. //
  72. // Author: S. Deodato ( 04.03.94)
  73. //
  74. //---------------------------------------------------------------------------
  75.  
  76. FzNum::FzNum( double value)
  77.  
  78. {
  79.    for ( int i = 0; i <= NPLEV; i++)
  80.       ciPLev[ i] = value;
  81. }
  82.  
  83.  
  84. //---------------------------------------------------------------------------
  85. //
  86. // FzNum::FzNum( double left, double middle, double right)
  87. // 
  88. //    Triangular fuzzy number (T.F.N.) constructor.
  89. //
  90. // Arguments:
  91. //
  92. //    double left
  93. //       T.F.N. lower bound
  94. //
  95. //    double middle
  96. //       T.F.N. maximum of presumption value
  97. //
  98. //    double right
  99. //       T.F.N. upper bound
  100. //
  101. // Return value:
  102. //
  103. //    a new fuzzy number
  104. //
  105. // Side effects:
  106. //
  107. //    none
  108. //
  109. // Author: S. Deodato ( 03.03.94)
  110. //
  111. //---------------------------------------------------------------------------
  112.  
  113. FzNum::FzNum( double left, double middle, double right)
  114.  
  115. {
  116.    ciPLev[ 0] = ConfInt( left, right);
  117.    ciPLev[ NPLEV] = ConfInt( middle);
  118.  
  119.    double tmp1 = middle - left;
  120.    double tmp2 = right - middle;
  121.  
  122.    for ( int i = 1; i < NPLEV; i++) {
  123.  
  124.       double tmp3 = double( i) / NPLEV;
  125.       ciPLev[ i] = ConfInt( left + tmp1 * tmp3,
  126.                             right - tmp2 * tmp3);
  127.    }
  128. }
  129.  
  130.  
  131. //---------------------------------------------------------------------------
  132. //
  133. // FzNum::FzNum( double lBase, double lTop, double rTop, double rBase)
  134. // 
  135. //    Trapezoidal fuzzy number constructor.
  136. //
  137. // Arguments:
  138. //
  139. //    double lBase
  140. //    double rBase
  141. //       confidence interval at presumption level 0
  142. //
  143. //    double lTop
  144. //    double rTop
  145. //       confidence interval at presumption level 1
  146. //
  147. // Return value:
  148. //
  149. //    a new fuzzy number
  150. //
  151. // Side effects:
  152. //
  153. //    none
  154. //
  155. // Author: S. Deodato ( 26.08.94)
  156. //
  157. //---------------------------------------------------------------------------
  158.  
  159. FzNum::FzNum( double lBase, double lTop, double rTop, double rBase)
  160.  
  161. {
  162.    ciPLev[ 0] = ConfInt( lBase, rBase);
  163.    ciPLev[ NPLEV] = ConfInt( lTop, rTop);
  164.  
  165.    double tmp1 = lTop - lBase;
  166.    double tmp2 = rBase - rTop;
  167.  
  168.    for ( int i = 1; i < NPLEV; i++) {
  169.  
  170.       double tmp3 = double( i) / NPLEV;
  171.       ciPLev[ i] = ConfInt( lBase + tmp1 * tmp3,
  172.                             rBase - tmp2 * tmp3);
  173.    }
  174. }
  175.  
  176.  
  177. //---------------------------------------------------------------------------
  178. //
  179. // double FzNum::operator[]( double value)
  180. //
  181. //    Overload the subscripting operator to compute the presumption level
  182. //    used to assert that a given value is represented by a fuzzy number.
  183. //
  184. // Arguments:
  185. //
  186. //    double value
  187. //       the value to work on
  188. //
  189. // Return value:
  190. //
  191. //    the computed presumption level
  192. //
  193. // Side effects:
  194. //
  195. //    none
  196. //
  197. // Author: S. Deodato ( 27.08.94)
  198. //
  199. //---------------------------------------------------------------------------
  200.  
  201. double FzNum::operator[]( double value)
  202.  
  203. {
  204.    if (ciPLev[ 0][ value]) {
  205.  
  206.       double pLev;
  207.       int i;
  208.  
  209.       for ( i = 1; i < NPLEV && ciPLev[ i][ value]; i++);
  210.  
  211.       if ( value < ciPLev[ i].GetLower())
  212.  
  213.          pLev = double( i - 1) / NPLEV 
  214.                 + ( value - ciPLev[ i - 1].GetLower()) 
  215.                   / ( NPLEV 
  216.                     * ( ciPLev[ i].GetLower() - ciPLev[ i - 1].GetLower()));
  217.  
  218.       else if ( value > ciPLev[ i].GetUpper())
  219.  
  220.          pLev = double( i) / NPLEV
  221.                 - ( value - ciPLev[ i].GetUpper())
  222.                   / ( NPLEV
  223.                     * ( ciPLev[ i].GetUpper() - ciPLev[ i - 1].GetUpper()));
  224.  
  225.       else
  226.  
  227.          pLev = 1;
  228.  
  229.       return pLev;
  230.    }
  231.    else
  232.       return 0;
  233. }
  234.  
  235.  
  236. //---------------------------------------------------------------------------
  237. //
  238. // FzNum FzNum::operator-()
  239. //
  240. //    Apply the unary minus operator to a fuzzy number.
  241. //
  242. // Arguments:
  243. //
  244. //    none
  245. //
  246. // Return value:
  247. //
  248. //    the resulting fuzzy number
  249. //
  250. // Side effects:
  251. //
  252. //    none
  253. //
  254. // Author: S. Deodato ( 04.03.94)
  255. //
  256. //---------------------------------------------------------------------------
  257.  
  258. FzNum FzNum::operator-()
  259.  
  260. {
  261.    FzNum fzTmp;
  262.  
  263.    for ( int i = 0; i <= NPLEV; i++)
  264.       fzTmp.ciPLev[ i] = -ciPLev[ i];
  265.  
  266.    return fzTmp;
  267. }
  268.  
  269.  
  270. //---------------------------------------------------------------------------
  271. //
  272. // FzNum FzNum::operator+( double opnd2)
  273. //
  274. //    Addiction between a singleton and a fuzzy number.
  275. //
  276. // Arguments:
  277. //
  278. //    double opnd2
  279. //       singleton to add to
  280. //
  281. // Return value:
  282. //
  283. //    the resulting fuzzy number
  284. //
  285. // Side effects:
  286. //
  287. //    none
  288. //
  289. // Author: S. Deodato ( 04.03.94)
  290. //
  291. //---------------------------------------------------------------------------
  292.  
  293. FzNum FzNum::operator+( double opnd2)
  294.  
  295. {
  296.    FzNum fzTmp;
  297.  
  298.    for ( int i = 0; i <= NPLEV; i++)
  299.       fzTmp.ciPLev[ i] = ciPLev[ i] + opnd2;
  300.  
  301.    return fzTmp;
  302. }
  303.  
  304.  
  305. //---------------------------------------------------------------------------
  306. //
  307. // FzNum FzNum::operator+( FzNum opnd2)
  308. //
  309. //    Addiction between two fuzzy numbers.
  310. //
  311. // Arguments:
  312. //
  313. //    FzNum opnd2
  314. //       FzNum number to add to
  315. //
  316. // Return value:
  317. //
  318. //    the resulting fuzzy number
  319. //
  320. // Side effects:
  321. //
  322. //    none
  323. //
  324. // Author: S. Deodato ( 04.03.94)
  325. //
  326. //---------------------------------------------------------------------------
  327.  
  328. FzNum FzNum::operator+( FzNum opnd2)
  329.  
  330. {
  331.    FzNum fzTmp;
  332.  
  333.    for ( int i = 0; i <= NPLEV; i++)
  334.       fzTmp.ciPLev[ i] = ciPLev[ i] + opnd2.ciPLev[ i];
  335.  
  336.    return fzTmp;
  337. }
  338.  
  339.  
  340. //---------------------------------------------------------------------------
  341. //
  342. // FzNum FzNum::operator-( double opnd2)
  343. //
  344. //    Subtraction between a singleton and a fuzzy number.
  345. //
  346. // Arguments:
  347. //
  348. //    double opnd2
  349. //       singleton to subtract from
  350. //
  351. // Return value:
  352. //
  353. //    the resulting fuzzy number
  354. //
  355. // Side effects:
  356. //
  357. //    none
  358. //
  359. // Author: S. Deodato ( 04.03.94)
  360. //
  361. //---------------------------------------------------------------------------
  362.  
  363. FzNum FzNum::operator-( double opnd2)
  364.  
  365. {
  366.    FzNum fzTmp;
  367.  
  368.    for ( int i = 0; i <= NPLEV; i++)
  369.       fzTmp.ciPLev[ i] = ciPLev[ i] - opnd2;
  370.  
  371.    return fzTmp;
  372. }
  373.  
  374.  
  375. //---------------------------------------------------------------------------
  376. //
  377. // FzNum FzNum::operator-( FzNum opnd2)
  378. //
  379. //    Subtraction between two fuzzy numbers.
  380. //
  381. // Arguments:
  382. //
  383. //    FzNum opnd2
  384. //       fuzzy number to subtract from 
  385. //
  386. // Return value:
  387. //
  388. //    the resulting fuzzy number
  389. //
  390. // Side effects:
  391. //
  392. //    none
  393. //
  394. // Author: S. Deodato ( 04.03.94)
  395. //
  396. //---------------------------------------------------------------------------
  397.  
  398. FzNum FzNum::operator-( FzNum opnd2)
  399.  
  400. {
  401.    FzNum fzTmp;
  402.  
  403.    for ( int i = 0; i <= NPLEV; i++)
  404.       fzTmp.ciPLev[ i] = ciPLev[ i] - opnd2.ciPLev[ i];
  405.  
  406.    return fzTmp;
  407. }
  408.  
  409.  
  410. //---------------------------------------------------------------------------
  411. //
  412. // FzNum FzNum::operator*( double opnd2)
  413. //
  414. //    Multiplication between a singleton and a fuzzy number.
  415. //
  416. // Arguments:
  417. //
  418. //    double opnd2
  419. //       singleton to multiply for
  420. //
  421. // Return value:
  422. //
  423. //    the resulting fuzzy number
  424. //
  425. // Side effects:
  426. //
  427. //    none
  428. //
  429. // Author: S. Deodato ( 04.03.94)
  430. //
  431. //---------------------------------------------------------------------------
  432.  
  433. FzNum FzNum::operator*( double opnd2)
  434.  
  435. {
  436.    FzNum fzTmp;
  437.  
  438.    for ( int i = 0; i <= NPLEV; i++)
  439.       fzTmp.ciPLev[ i] = ciPLev[ i] * opnd2;
  440.  
  441.    return fzTmp;
  442. }
  443.  
  444.  
  445. //---------------------------------------------------------------------------
  446. //
  447. // FzNum FzNum::operator*( FzNum opnd2)
  448. //
  449. //    Multiplication between two fuzzy numbers.
  450. //
  451. // Arguments:
  452. //
  453. //    FzNum opnd2
  454. //       fuzzy number to multiply for
  455. //
  456. // Return value:
  457. //
  458. //    the resulting fuzzy number
  459. //
  460. // Side effects:
  461. //
  462. //    none
  463. //
  464. // Author: S. Deodato ( 04.03.94)
  465. //
  466. //---------------------------------------------------------------------------
  467.  
  468. FzNum FzNum::operator*( FzNum opnd2)
  469.  
  470. {
  471.    FzNum fzTmp;
  472.  
  473.    for ( int i = 0; i <= NPLEV; i++)
  474.       fzTmp.ciPLev[ i] = ciPLev[ i] * opnd2.ciPLev[ i];
  475.  
  476.    return fzTmp;
  477. }
  478.  
  479.  
  480. //---------------------------------------------------------------------------
  481. //
  482. // FzNum FzNum::operator/( double opnd2)
  483. //
  484. //    Division between a singleton and a fuzzy number.
  485. //
  486. // Arguments:
  487. //
  488. //    double opnd2
  489. //       singleton to divide by
  490. //
  491. // Return value:
  492. //
  493. //    the resulting fuzzy number
  494. //
  495. // Side effects:
  496. //
  497. //    none
  498. //
  499. // Author: S. Deodato ( 04.03.94)
  500. //
  501. //---------------------------------------------------------------------------
  502.  
  503. FzNum FzNum::operator/( double opnd2)
  504.  
  505. {
  506.    FzNum fzTmp;
  507.  
  508.    for ( int i = 0; i <= NPLEV; i++)
  509.       fzTmp.ciPLev[ i] = ciPLev[ i] / opnd2;
  510.  
  511.    return fzTmp;
  512. }
  513.  
  514.  
  515. //---------------------------------------------------------------------------
  516. //
  517. // FzNum FzNum::operator/( FzNum opnd2)
  518. //
  519. //    Division between two fuzzy numbers.
  520. //
  521. // Arguments:
  522. //
  523. //    double opnd2
  524. //       fuzzy number to divide by 
  525. //
  526. // Return value:
  527. //
  528. //    the resulting fuzzy number
  529. //
  530. // Side effects:
  531. //
  532. //    none
  533. //
  534. // Author: S. Deodato ( 04.03.94)
  535. //
  536. //---------------------------------------------------------------------------
  537.  
  538. FzNum FzNum::operator/( FzNum opnd2)
  539.  
  540. {
  541.    FzNum fzTmp;
  542.  
  543.    for ( int i = 0; i <= NPLEV; i++)
  544.       fzTmp.ciPLev[ i] = ciPLev[ i] / opnd2.ciPLev[ i];
  545.  
  546.    return fzTmp;
  547. }
  548.  
  549.  
  550. //---------------------------------------------------------------------------
  551. //
  552. // FzNum FzNum::operator+=( double opnd2)
  553. //
  554. //    Addiction and assignment between a singleton and a fuzzy number.
  555. //
  556. // Arguments:
  557. //
  558. //    double opnd2
  559. //       singleton to add to
  560. //
  561. // Return value:
  562. //
  563. //    the resulting fuzzy number
  564. //
  565. // Side effects:
  566. //
  567. //    updates the current fuzzy number
  568. //
  569. // Author: S. Deodato ( 04.03.94)
  570. //
  571. //---------------------------------------------------------------------------
  572.  
  573. FzNum FzNum::operator+=( double opnd2)
  574.  
  575. {
  576.    for ( int i = 0; i <= NPLEV; i++)
  577.       ciPLev[ i] += opnd2;
  578.  
  579.    return *this;
  580. }
  581.  
  582.  
  583. //---------------------------------------------------------------------------
  584. //
  585. // FzNum FzNum::operator+=( FzNum opnd2)
  586. //
  587. //    Addiction and assignment between two fuzzy numbers.
  588. //
  589. // Arguments:
  590. //
  591. //    FzNum opnd2
  592. //       fuzzy number to add to
  593. //
  594. // Return value:
  595. //
  596. //    the resulting fuzzy number
  597. //
  598. // Side effects:
  599. //
  600. //    updates the current fuzzy number
  601. //
  602. // Author: S. Deodato ( 04.03.94)
  603. //
  604. //---------------------------------------------------------------------------
  605.  
  606. FzNum FzNum::operator+=( FzNum opnd2)
  607.  
  608. {
  609.    for ( int i = 0; i <= NPLEV; i++)
  610.       ciPLev[ i] += opnd2.ciPLev[ i];
  611.  
  612.    return *this;
  613. }
  614.  
  615.  
  616. //---------------------------------------------------------------------------
  617. //
  618. // FzNum FzNum::operator-=( double opnd2)
  619. //
  620. //    Subtraction and assignment between a singleton and a fuzzy number.
  621. //
  622. // Arguments:
  623. //
  624. //    double opnd2
  625. //       singleton to subtract from
  626. //
  627. // Return value:
  628. //
  629. //    the resulting fuzzy number
  630. //
  631. // Side effects:
  632. //
  633. //    updates the current fuzzy number
  634. //
  635. // Author: S. Deodato ( 04.03.94)
  636. //
  637. //---------------------------------------------------------------------------
  638.  
  639. FzNum FzNum::operator-=( double opnd2)
  640.  
  641. {
  642.    for ( int i = 0; i <= NPLEV; i++)
  643.       ciPLev[ i] -= opnd2;
  644.  
  645.    return *this;
  646. }
  647.  
  648.  
  649. //---------------------------------------------------------------------------
  650. //
  651. // FzNum FzNum::operator-=( FzNum opnd2)
  652. //
  653. //    Subtraction and assignment between two fuzzy numbers.
  654. //
  655. // Arguments:
  656. //
  657. //    FzNum opnd2
  658. //       fuzzy number to subtract from 
  659. //
  660. // Return value:
  661. //
  662. //    the resulting fuzzy number
  663. //
  664. // Side effects:
  665. //
  666. //    updates the current fuzzy number
  667. //
  668. // Author: S. Deodato ( 04.03.94)
  669. //
  670. //---------------------------------------------------------------------------
  671.  
  672. FzNum FzNum::operator-=( FzNum opnd2)
  673.  
  674. {
  675.    for ( int i = 0; i <= NPLEV; i++)
  676.       ciPLev[ i] -= opnd2.ciPLev[ i];
  677.  
  678.    return *this;
  679. }
  680.  
  681.  
  682. //---------------------------------------------------------------------------
  683. //
  684. // FzNum FzNum::operator*=( double opnd2)
  685. //
  686. //    Multiplication and assignment between a singleton and a fuzzy number.
  687. //
  688. // Arguments:
  689. //
  690. //    double opnd2
  691. //       singleton to multiply for
  692. //
  693. // Return value:
  694. //
  695. //    the resulting fuzzy number
  696. //
  697. // Side effects:
  698. //
  699. //    updates the current fuzzy number
  700. //
  701. // Author: S. Deodato ( 04.03.94)
  702. //
  703. //---------------------------------------------------------------------------
  704.  
  705. FzNum FzNum::operator*=( double opnd2)
  706.  
  707. {
  708.    for ( int i = 0; i <= NPLEV; i++)
  709.       ciPLev[ i] *= opnd2;
  710.  
  711.    return *this;
  712. }
  713.  
  714.  
  715. //---------------------------------------------------------------------------
  716. //
  717. // FzNum FzNum::operator*=( FzNum opnd2)
  718. //
  719. //    Multiplication and assignment between two fuzzy numbers.
  720. //
  721. // Arguments:
  722. //
  723. //    FzNum opnd2
  724. //       fuzzy number to multiply for 
  725. //
  726. // Return value:
  727. //
  728. //    the resulting fuzzy number
  729. //
  730. // Side effects:
  731. //
  732. //    updates the current fuzzy number
  733. //
  734. // Author: S. Deodato ( 04.03.94)
  735. //
  736. //---------------------------------------------------------------------------
  737.  
  738. FzNum FzNum::operator*=( FzNum opnd2)
  739.  
  740. {
  741.    for ( int i = 0; i <= NPLEV; i++)
  742.       ciPLev[ i] *= opnd2.ciPLev[ i];
  743.  
  744.    return *this;
  745. }
  746.  
  747.  
  748. //---------------------------------------------------------------------------
  749. //
  750. // FzNum FzNum::operator/=( double opnd2)
  751. //
  752. //    Division and assignment between a singleton and a fuzzy number.
  753. //
  754. // Arguments:
  755. //
  756. //    double opnd2
  757. //       singleton to divide by
  758. //
  759. // Return value:
  760. //
  761. //    the resulting fuzzy number
  762. //
  763. // Side effects:
  764. //
  765. //    updates the current fuzzy number
  766. //
  767. // Author: S. Deodato ( 04.03.94)
  768. //
  769. //---------------------------------------------------------------------------
  770.  
  771. FzNum FzNum::operator/=( double opnd2)
  772.  
  773. {
  774.    for ( int i = 0; i <= NPLEV; i++)
  775.       ciPLev[ i] /= opnd2;
  776.  
  777.    return *this;
  778. }
  779.  
  780.  
  781. //---------------------------------------------------------------------------
  782. //
  783. // FzNum FzNum::operator/=( FzNum opnd2)
  784. //
  785. //    Division and assignment between two fuzzy numbers.
  786. //
  787. // Arguments:
  788. //
  789. //    FzNum opnd2
  790. //       fuzzy number to divide by 
  791. //
  792. // Return value:
  793. //
  794. //    the resulting fuzzy number
  795. //
  796. // Side effects:
  797. //
  798. //    updates the current fuzzy number
  799. //
  800. // Author: S. Deodato ( 04.03.94)
  801. //
  802. //---------------------------------------------------------------------------
  803.  
  804. FzNum FzNum::operator/=( FzNum opnd2)
  805.  
  806. {
  807.    for ( int i = 0; i <= NPLEV; i++)
  808.       ciPLev[ i] /= opnd2.ciPLev[ i];
  809.  
  810.    return *this;
  811. }
  812.  
  813.  
  814. //---------------------------------------------------------------------------
  815. //
  816. // FzNum operator/( double opnd1, FzNum opnd2)
  817. //
  818. //    Division between a singleton and a fuzzy number.
  819. //
  820. // Arguments:
  821. //
  822. //    double opnd1
  823. //    FzNum opnd2
  824. //       division operands
  825. //
  826. // Return value:
  827. //
  828. //    the resulting fuzzy number
  829. //
  830. // Side effects:
  831. //
  832. //    none
  833. //
  834. // Author: S. Deodato ( 06.03.94)
  835. //
  836. //---------------------------------------------------------------------------
  837.  
  838. FzNum operator/( double opnd1, FzNum opnd2)
  839.  
  840. {
  841.    FzNum fzTmp;
  842.  
  843.    for ( int i = 0; i <= NPLEV; i++)
  844.       fzTmp.ciPLev[ i] = opnd1 / opnd2.ciPLev[ i];
  845.  
  846.    return fzTmp;
  847. }
  848.  
  849.  
  850. //---------------------------------------------------------------------------
  851. //
  852. // ostream& operator<<( ostream& smStream, FzNum item)
  853. // 
  854. //    Write a fuzzy number into an output stream.
  855. //
  856. // Arguments:
  857. //
  858. //    ostream& smStream
  859. //       reference to stream to write on
  860. //
  861. //    FzNum item
  862. //       fuzzy number to write
  863. //
  864. // Return value:
  865. //
  866. //    reference to the used output stream
  867. //
  868. // Side effects:
  869. //
  870. //    none
  871. //
  872. // Author: S. Deodato ( 04.03.94)
  873. //
  874. //---------------------------------------------------------------------------
  875.  
  876. ostream& operator<<( ostream& smStream, FzNum item)
  877.  
  878. {
  879.    for ( int i = 0; i <= NPLEV; i++)
  880.     smStream << "Presumption level = " << setw( 5) << double( i) / NPLEV
  881.          << " => " << item.ciPLev[ i] << "\n";
  882.  
  883.    return smStream;
  884. }
  885.  
  886.  
  887. //---------------------------------------------------------------------------
  888. //
  889. // FzNum min( FzNum opnd1, FzNum opnd2)
  890. //
  891. //    Calculate the minimum fuzzy number from two given ones.
  892. //
  893. // Arguments:
  894. //
  895. //    FzNum opnd1
  896. //    FzNum opnd2
  897. //       the two given fuzzy numbers
  898. //
  899. // Return value:
  900. //
  901. //    the computed minimum
  902. //
  903. // Side effects:
  904. //
  905. //    none
  906. //
  907. // Author: S. Deodato ( 04.03.94)
  908. //
  909. //---------------------------------------------------------------------------
  910.  
  911. FzNum min( FzNum opnd1, FzNum opnd2)
  912.  
  913. {
  914.    FzNum fzTmp;
  915.  
  916.    for ( int i = 0; i <= NPLEV; i++)
  917.       fzTmp.ciPLev[ i] = min( opnd1.ciPLev[ i], opnd2.ciPLev[ i]);
  918.  
  919.    return fzTmp;
  920. }
  921.  
  922.  
  923. //---------------------------------------------------------------------------
  924. //
  925. // FzNum max( FzNum opnd1, FzNum opnd2)
  926. //
  927. //    Calculate the maximum fuzzy number from two given ones.
  928. //
  929. // Arguments:
  930. //
  931. //    FzNum opnd1
  932. //    FzNum opnd2
  933. //       the two given fuzzy numbers
  934. //
  935. // Return value:
  936. //
  937. //    the computed maximum
  938. //
  939. // Side effects:
  940. //
  941. //    none
  942. //
  943. // Author: S. Deodato ( 04.03.94)
  944. //
  945. //---------------------------------------------------------------------------
  946.  
  947. FzNum max( FzNum opnd1, FzNum opnd2)
  948.  
  949. {
  950.    FzNum fzTmp;
  951.  
  952.    for ( int i = 0; i <= NPLEV; i++)
  953.       fzTmp.ciPLev[ i] = max( opnd1.ciPLev[ i], opnd2.ciPLev[ i]);
  954.  
  955.    return fzTmp;
  956. }
  957.  
  958.  
  959. //---------------------------------------------------------------------------
  960. //
  961. // FzNum sin( FzNum opnd)
  962. //
  963. //    Calculate sine of fuzzy numbers.
  964. //
  965. // Arguments:
  966. //
  967. //    FzNum opnd
  968. //       a fuzzy number (expressed in radiants, not degrees)
  969. //
  970. // Return value:
  971. //
  972. //    the resulting fuzzy number
  973. //
  974. // Side effects:
  975. //
  976. //    none
  977. //
  978. // Author: S. Deodato ( 26.04.94)
  979. //
  980. //---------------------------------------------------------------------------
  981.  
  982. FzNum sin( FzNum opnd)
  983.  
  984. {
  985.    FzNum fzTmp;
  986.  
  987.    for ( int i = 0; i <= NPLEV; i++)
  988.       fzTmp.ciPLev[ i] = sin( opnd.ciPLev[ i]);
  989.  
  990.    return fzTmp;
  991. }
  992.  
  993.  
  994. //---------------------------------------------------------------------------
  995. //
  996. // FzNum cos( FzNum opnd)
  997. //
  998. //    Calculate cosine of fuzzy numbers.
  999. //
  1000. // Arguments:
  1001. //
  1002. //    FzNum opnd
  1003. //       a fuzzy number (expressed in radiants, not degrees)
  1004. //
  1005. // Return value:
  1006. //
  1007. //    the resulting fuzzy number
  1008. //
  1009. // Side effects:
  1010. //
  1011. //    none
  1012. //
  1013. // Author: S. Deodato ( 26.04.94)
  1014. //
  1015. //---------------------------------------------------------------------------
  1016.  
  1017. FzNum cos( FzNum opnd)
  1018.  
  1019. {
  1020.    FzNum fzTmp;
  1021.  
  1022.    for ( int i = 0; i <= NPLEV; i++)
  1023.       fzTmp.ciPLev[ i] = cos( opnd.ciPLev[ i]);
  1024.  
  1025.    return fzTmp;
  1026. }
  1027.  
  1028.  
  1029. //---------------------------------------------------------------------------
  1030. //
  1031. // double fzSM( FzNum opnd1, FzNum opnd2, double lambda)
  1032. //
  1033. //    Calculate a similarity measure between two fuzzy numbers.
  1034. //
  1035. //    This is a modified version of the dissemblance index to take account
  1036. //    of not only the dissemblance between the shapes of two fuzzy numbers
  1037. //    but also the separation of their centers, according to the choice of
  1038. //    the appropriate 'lambda' value.
  1039. //
  1040. //    Based on "Uncertain robot environment modelling using fuzzy numbers"
  1041. //    by W. J. Kim et al. in "Fuzzy Sets and Systems" n. 61 (1994),
  1042. //    North-Holland, pp. 53-62.
  1043. //
  1044. // Arguments:
  1045. //
  1046. //    FzNum opnd1
  1047. //    FzNum opnd2
  1048. //       the two fuzzy numbers to compare
  1049. //
  1050. //    double lambda
  1051. //       the value to balance the weight of the center values and of the
  1052. //       shapes of the fuzzy numbers (0 <= lambda <= 1, default value = 0.5)
  1053. //
  1054. // Return value:
  1055. //
  1056. //    the computed similarity measure
  1057. //
  1058. // Side effects:
  1059. //
  1060. //    none
  1061. //
  1062. // Author: S. Deodato ( 19.04.94)
  1063. //
  1064. //---------------------------------------------------------------------------
  1065.  
  1066.  
  1067. double fzSM( FzNum opnd1, FzNum opnd2, double lambda)
  1068.  
  1069. {
  1070.    if ( lambda < 0 || lambda > 1) {
  1071.  
  1072.       cerr << "\n\nfzSM: 'lambda' value not in [ 0, 1] ( lambda = "
  1073.            << lambda << ").\n";
  1074.  
  1075.       abort();
  1076.    }
  1077.  
  1078.    double normFactor = 2 * (
  1079.       (( opnd1.ciPLev[ 0].GetUpper() > opnd2.ciPLev[ 0].GetUpper())
  1080.          ? opnd1.ciPLev[ 0].GetUpper()
  1081.          : opnd2.ciPLev[ 0].GetUpper()) -
  1082.       (( opnd1.ciPLev[ 0].GetLower() < opnd2.ciPLev[ 0].GetLower())
  1083.          ? opnd1.ciPLev[ 0].GetLower()
  1084.          : opnd2.ciPLev[ 0].GetLower()));
  1085.  
  1086.    double sm = 0;
  1087.  
  1088.    for ( int i = 0; i <= NPLEV; i++)
  1089.       sm += ciDelta( opnd1.ciPLev[ i], opnd2.ciPLev[ i]);
  1090.  
  1091.    sm = lambda * (( sm / normFactor) / ( NPLEV + 1));
  1092.  
  1093.    sm += (( 1 - lambda) *
  1094.             ( ciDelta( opnd1.ciPLev[ NPLEV], opnd2.ciPLev[ NPLEV]) /
  1095.               normFactor));
  1096.  
  1097.    return sm;
  1098. }
  1099.  
  1100.  
  1101. //---------------------------------------------------------------------------
  1102. //
  1103. // void FzNum::print()
  1104. //
  1105. //    Print the fuzzy number internal representation into the standard
  1106. //    output.
  1107. //
  1108. // Arguments:
  1109. //
  1110. //    none
  1111. //
  1112. // Return value:
  1113. //
  1114. //    none
  1115. //
  1116. // Side effects:
  1117. //
  1118. //    none
  1119. //
  1120. // Author: S. Deodato ( 08.07.94)
  1121. //
  1122. //---------------------------------------------------------------------------
  1123.  
  1124. void FzNum::print()
  1125.  
  1126. {
  1127.    int i;
  1128.    float x;
  1129.    float delta = float( 1) / NPLEV;
  1130.  
  1131.    cout << "\n\n";
  1132.  
  1133.    for ( i = 0, x = 0; i <= NPLEV; i++, x += delta)
  1134.       cout << setprecision( 6) << ciPLev[ i].GetLower() << "\t"
  1135.            << setw( 5) <<x << "\n";
  1136.  
  1137.    for ( i = NPLEV, x-=delta; i >= 0; i--, x -= delta)
  1138.       cout << setprecision( 6) << ciPLev[ i].GetUpper() << "\t"
  1139.            << setw( 5) << x << "\n";
  1140. }
  1141.